from unittest import TestCase

from comp_arch.speedup import get_speedup, get_speedup_alt


class TestSpeedup(TestCase):
    def test_get_speedup_page50(self):
        fraction_unchanged = 0.6
        fraction_enhancement = 0.4
        speed_enhancement = 10
        expected_speedup = 1.56
        result = get_speedup(
            fraction_enhancement, speed_enhancement, fraction_unchanged
        )
        self.assertEqual(round(result, 2), expected_speedup)

    def test_get_speedup_q1An2p1(self):
        fraction_unchanged = 0.2
        fraction_enhancement = 0.8
        speed_enhancement = 2
        expected_speedup = 1.67
        result = get_speedup(
            fraction_enhancement, speed_enhancement, fraction_unchanged
        )
        self.assertEqual(round(result, 2), expected_speedup)

    def test_get_speedup_q1An2p2(self):
        # NOTE: For this one we doubled the performance of everything.
        fraction_unchanged = 0
        fraction_enhancement = 1
        speed_enhancement = 2
        expected_speedup = 2
        result = get_speedup(
            fraction_enhancement, speed_enhancement, fraction_unchanged
        )
        self.assertEqual(round(result, 2), expected_speedup)

    def test_get_speedup_q1Bn2(self):
        fraction_unchanged = 0.4
        fraction_enhancement = 0.6
        speed_enhancement = 2
        expected_speedup = 1.43
        result = get_speedup(
            fraction_enhancement, speed_enhancement, fraction_unchanged
        )
        self.assertEqual(round(result, 2), expected_speedup)


class TestSpeedupMcasesActual(TestCase):
    def test_get_speedup_q1n2p1_alt(self):
        fraction_enhancement = 0.4
        speed_enhancement = 2
        expected_speedup = 1.25
        result = get_speedup_alt(fraction_enhancement, speed_enhancement)
        self.assertEqual(round(result, 2), expected_speedup)

    def test_get_speedup_q1n2p1(self):
        fraction_enhancement = 0.4
        fraction_unchanged = 1 - fraction_enhancement
        speed_enhancement = 2
        expected_speedup = 1.25
        result = get_speedup(
            fraction_enhancement, speed_enhancement, fraction_unchanged
        )
        self.assertEqual(round(result, 2), expected_speedup)

    def test_get_speedup_q1n2p2(self):
        fraction_enhancement = 0.6
        fraction_unchanged = 1 - fraction_enhancement
        speed_enhancement = 1.25
        expected_speedup = 1.14
        result = get_speedup(
            fraction_enhancement, speed_enhancement, fraction_unchanged
        )
        self.assertEqual(round(result, 2), expected_speedup)
